﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using MS.InformationManagement.DAL.DTOs;
using MS.InformationManagement.Presenter.Support;
using MS.InformationManagement.Presenter.ShippingControByUser;
using Microsoft.SharePoint;
using MS.InformationManagement.DAL.DocumentCenter;
using System.Collections.Generic;
using System.Text;

namespace MS.InformationManagement.WP.DocumentCenter.ShippingControByUser
{
    public partial class ShippingControByUserUserControl : BaseControl, IShippingControByUserView
    {

        #region fields

        ShippingControByUserPresenter _presenter;

        #endregion

        #region Properties

        public string IdRadicado { get; set; }
        public IEnumerable<Devolucion> foundRadicados { get; set; }
        string _queryString = string.Empty;

        #endregion

        #region Form Events

        protected void Page_Load(object sender, EventArgs e)
        {
            _presenter = new ShippingControByUserPresenter(this);
            _presenter.HandleLoad();
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {

        }

        protected void gvRadicados_RowCreated(object sender, GridViewRowEventArgs e)
        {

        }

        protected void gvRadicados_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            try
            {
                if (e.Row.RowType != DataControlRowType.EmptyDataRow)
                {
                    e.Row.Cells[0].Visible = false;
                    e.Row.Cells[1].Visible = false;
                    e.Row.Cells[2].Visible = false;
                    e.Row.Cells[3].Visible = false;
                }

                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    ImageButton btnImprimir = (ImageButton)e.Row.FindControl("btnImprimir");
                    if (btnImprimir != null)
                    {
                        btnImprimir.ID = "btnImprimir_" + e.Row.DataItemIndex;
                        btnImprimir.Click +=new ImageClickEventHandler(btnImprimir_Click);
                    }

                    Button btnSubmitOperation = (Button)e.Row.FindControl("btnSubmitOperation");
                    if (btnSubmitOperation != null)
                    {
                        btnSubmitOperation.ID = "lblRadicado_" + e.Row.DataItemIndex;
                        btnSubmitOperation.Click +=new EventHandler(btnSubmitOperation_Click);
                    }

                    CheckBox chk = (CheckBox)e.Row.FindControl("chkCopia");
                    HyperLink lblRadicado = e.Row.FindControl("HyperLinkID") as HyperLink;
                    if (lblRadicado != null)
                    {
                        if (chk.Checked)
                            _queryString = "UpdateCopy&IdCopy=" + e.Row.Cells[2].Text;
                        else
                            _queryString = "UpdateRemitente";

                        string _Url = string.Format("/_layouts/MS.InformationManagement.WP/InsertarRadicadoSalida.aspx?strategy={0}&List={1}&ID={2}", _queryString, SPContext.Current.Web.Lists[e.Row.Cells[0].Text].ID, e.Row.Cells[1].Text);
                        lblRadicado.NavigateUrl = SPContext.Current.Web.Url + _Url;
                    }
                }
            }
            catch (Exception ex)
            {
                HandleError(ex);
            }
        }

        protected void btnSubmitOperation_Click(object sender, EventArgs e)
        {
            try 
            {
                Button btn = (Button)sender;
                GridViewRow row = (GridViewRow)btn.NamingContainer;
                string url = string.Format(SPContext.Current.Web.Url + "/_layouts/MS.InformationManagement.WP/CancelShipping.aspx?List={0}&ID={1}&IdDevolucion={2}", SPContext.Current.Web.Lists[row.Cells[0].Text].ID, row.Cells[2].Text, row.Cells[3].Text);
                DesplegarVentanaModal(url, "Sistema de Gestión Documental", 600, 260, this.Page);
            }
            catch (Exception ex)
            {
                HandleError(ex);
            }
        }

        protected void btnImprimir_Click(object sender, ImageClickEventArgs e)
        {
            ImageButton btn = (ImageButton)sender;
            GridViewRow row = (GridViewRow)btn.NamingContainer;
            CheckBox chkSelect = (CheckBox)row.FindControl("chkCopia");
            string url = string.Format(SPContext.Current.Web.Url + "/_layouts/MS.InformationManagement.WP/ConfirmationMessage.aspx?List={0}&ID={1}&IsCopy={2}&strategy={3}&IdDevolucion={4}", SPContext.Current.Web.Lists[row.Cells[0].Text].ID, row.Cells[2].Text, Convert.ToInt32(chkSelect.Checked), "SendToPrint", row.Cells[3].Text);
            DesplegarVentanaModal(url, "Sistema de Gestión Documental", 600, 260, this.Page);
        }

        #endregion

        #region Implemented Methods

        public void Show()
        {
            try
            {
                if (foundRadicados != null)
                {
                    gvRadicados.DataSource = foundRadicados;
                    gvRadicados.DataBind();
                }
            }
            catch (Exception ex)
            {
                HandleError(ex);
            }
        }

        public void DesplegarVentanaModal(string Url, string Titulo, Int32 ancho, Int32 alto, Page pagina)
        {
            StringBuilder scriptModal = new StringBuilder();
            scriptModal.Append("var showMyModalDialog = function() { var options = { ");
            scriptModal.Append("url : '");
            scriptModal.Append(Url);
            scriptModal.Append("', ");
            scriptModal.Append("title: '");
            scriptModal.Append(Titulo);
            scriptModal.Append("', width: ");
            scriptModal.Append(ancho.ToString());
            scriptModal.Append(", height: ");
            scriptModal.Append(alto.ToString());
            scriptModal.Append(", dialogReturnValueCallback: onDialogClose }; SP.UI.ModalDialog.commonModalDialogOpen(" + "'" + Url + "'" + ",options, onDialogClose, null); }; ");
            scriptModal.Append("function onDialogClose(dialogResult, returnValue) { ");
            scriptModal.Append("if (dialogResult == SP.UI.DialogResult.OK) { ");
            scriptModal.Append(" SP.UI.ModalDialog.RefreshPage(SP.UI.DialogResult.OK); } ");
            scriptModal.Append(" } ");

            pagina.ClientScript.RegisterClientScriptBlock(pagina.GetType(), "Mensaje", scriptModal.ToString(), true);
            pagina.ClientScript.RegisterStartupScript(typeof(Page), "Precarga", "ExecuteOrDelayUntilScriptLoaded(showMyModalDialog, 'sp.ui.dialog.js');", true);
        }

        #endregion

    }
}
